package com.huluobo.a2109mouthdemo.login;

import android.graphics.Paint;

import com.huluobo.a2109mouthdemo.IMApp;
import com.huluobo.a2109mouthdemo.base.BasePresenter;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

/**
 * Created by LC on 2023/12/1.
 */
public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter {
    @Override
    public void login(String username, String password) {
        if (username.matches(IMApp.REGEX_USERNAME)) {
            if (password.matches(IMApp.REGEX_PASSWORD)) {
                if (isAttach()) {
                    getView().onStartLogin();
                    LoginEMClient(username, password);
                }
            } else {
                if (isAttach()) {
                    getView().onPasswordError();

                }
            }
        } else {
            if (isAttach()) {
                getView().onUsernameError();
            }
        }
    }

    private void LoginEMClient(String username, String password) {
        EMClient.getInstance().login(username, password, new EMCallBack() {
            @Override
            public void onSuccess() {
                if (isAttach()) {
                    getView().onLoginSuccess();
                }
            }

            @Override
            public void onError(int code, String error) {
                if (isAttach()) {
                    getView().onLoginFailed(error);
                }
            }
        });
    }
}
